package mia.recommender.ch02;

import java.util.List;

import org.apache.mahout.cf.taste.impl.model.jdbc.PostgreSQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.postgresql.ds.PGPoolingDataSource;

import co.edu.usbcali.lidis.dataaccess.daoFactory.FachadaBaseDeDatos;
import co.edu.usbcali.lidis.modelo.Proyecto;
import co.edu.usbcali.lidis.vista.BusinessDelegate;


public class FormPropuesta {

	protected Shell shell;
	private Text txtPalClave;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			FormPropuesta window = new FormPropuesta();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		
		txtPalClave = new Text(shell, SWT.BORDER);
		txtPalClave.setBounds(197, 133, 190, 21);
		
		Label lblPalabrasClaves = new Label(shell, SWT.NONE);
		lblPalabrasClaves.setBounds(72, 136, 97, 15);
		lblPalabrasClaves.setText("Palabras Claves");
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String palabraClave = "";
				int retornoFuncion = 0;
				Long codProyectoRecom = 0L;
				try {
					palabraClave = txtPalClave.getText();
					retornoFuncion=FachadaBaseDeDatos.getInstance().ejecutaModelo(palabraClave);
					
					if (retornoFuncion==0) {
						System.out.println("Ejecución exitosa");
					}
					   PGPoolingDataSource dataSource = new PGPoolingDataSource();
					   dataSource.setDataSourceName("DataSourcepostgres");
					   dataSource.setServerName("localhost");
					   dataSource.setDatabaseName("tesis");
					   dataSource.setUser("postgres");
					   dataSource.setPassword("P0stgressql");
					   dataSource.setMaxConnections(10);
					   
					   JDBCDataModel dataModel = new PostgreSQLJDBCDataModel(dataSource, "RECOMENDACION", "USU_ID",
					     "PRO_ID", "PUN_VALOR", null);
					   
					  //DataModel model = new FileDataModel(modelFile);

				    UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
				    UserNeighborhood neighborhood =
				      new NearestNUserNeighborhood(2, similarity, dataModel);

				    Recommender recommender = new GenericUserBasedRecommender(
				    		dataModel, neighborhood, similarity);

				    List<RecommendedItem> recommendations = recommender.recommend(1,1);

				    for (RecommendedItem recommendation : recommendations) {
				    	codProyectoRecom=recommendation.getItemID();
				      System.out.println(codProyectoRecom);
				    }
	
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(79, 180, 75, 25);
		btnNewButton.setText("New Button");
	}
}
